package com.xyzq.common.csv;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvBeanReader;
import org.supercsv.io.ICsvBeanReader;
import org.supercsv.prefs.CsvPreference;

public class AnalyseCSV {

	private HashMap<String, CsvCell> cellMap;

	public ArrayList<Object> analyse(String csv, File file, Object object)
			throws Exception {
		ArrayList<Object> result = new ArrayList<Object>();
		BufferedReader br = new BufferedReader(new InputStreamReader(
				new FileInputStream(file), "UTF-8"));
		ICsvBeanReader inFile = new CsvBeanReader(br,
				CsvPreference.EXCEL_PREFERENCE);
		String[] header = getCellMap().get(csv).getHeader();
		CellProcessor[] cell = getCellMap().get(csv).getCell();
		while ((object = inFile.read(object.getClass(), header, cell)) != null) {
			result.add(object);
		}
		inFile.close();
		return result;
	}

	public void setCellMap(HashMap<String, CsvCell> cellMap) {
		this.cellMap = cellMap;
	}

	public HashMap<String, CsvCell> getCellMap() {
		return cellMap;
	}

}
